package com.example.demo.serviceimpl;

import com.example.demo.entity.Book;
import com.example.demo.entity.Category;
import com.example.demo.repository.BookRepository;
import com.example.demo.repository.CategoryRepository;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImp implements BookService {

    @Autowired
    BookRepository bookRepository;

    @Autowired
    CategoryRepository categoryRepository;

    @Override
    public void init() {
        Category category1 = new Category();
        category1.setName("IT技术");
        categoryRepository.save(category1);
        Category category2 = new Category();
        category2.setName("小说");
        categoryRepository.save(category2);


        Book b1 = new Book();
        b1.setName("JavaEE架构");
        b1.setIsbn("666666");
        b1.setAuthor("甲");
        b1.setPublish("一号出版社");
        b1.setPrice(30.0);
        b1.setCategory(category1);
        bookRepository.save(b1);
        Book b2 = new Book();
        b2.setName("Android编程技术");
        b2.setIsbn("777777");
        b2.setAuthor("乙");
        b2.setPublish("二号出版社");
        b2.setPrice(29.0);
        b2.setCategory(category1);
        bookRepository.save(b2);
        Book b3 = new Book();
        b3.setName("圣经");
        b3.setIsbn("888888");
        b3.setAuthor("甲");
        b3.setPublish("一号出版社");
        b3.setPrice(15.0);
        b3.setCategory(category2);
        bookRepository.save(b3);
        Book b4 = new Book();
        b4.setName("红楼梦");
        b4.setIsbn("999999");
        b4.setAuthor("甲");
        b4.setPublish("一号出版社");
        b4.setPrice(30.0);
        b4.setCategory(category2);
        bookRepository.save(b4);
        Book b5 = new Book();
        b5.setName("西游记");
        b5.setIsbn("123456");
        b5.setAuthor("甲");
        b5.setPublish("一号出版社");
        b5.setPrice(30.0);
        b5.setCategory(category2);
        bookRepository.save(b5);
    }

    @Override
    public Book getBookById(Long id) {
        return bookRepository.getOne(id);
    }

    @Override
    public List<Book> searchAll() {
        return bookRepository.findAll();
    }

    @Override
    public List<Book> searchByName(String title) {
        return bookRepository.findByNameLikeOrderByIdDesc("%"+title+"%");
    }

    @Override
    public List<Book> searchByPub(String name) {
        return bookRepository.findbookbypub(name);
    }

    @Override
    public List<Book> searchByCat(String cat) {
        return bookRepository.findbookbycat(cat);
    }


    @Override
    public void modify(Book book) {
        //int updateBook(String name, Category category,String author,String isbn,double price,String publish, Long book_id);
//        int count=bookRepository.updateBook(book.getName(),book.getCategory(),book.getAuthor(),book.getIsbn(),book.getPrice(),book.getPublish(),book.getId());
//        return count > 0 ? true : false;
        bookRepository.save(book);
    }
    @Override
    public void addbook(Book book) {
//        int count=bookRepository.addbook(book.getId(),book.getIsbn(),book.getName(),book.getAuthor(),book.getPublish(),book.getPrice(),book.getCategory().getId());
       bookRepository.save(book);
//        return count > 0 ? true : false;
    }

    @Override
    public Page<Book> getBookList(Pageable pageable) {
        return bookRepository.findAll(pageable);
    }

    @Override
    public boolean delete(String name) {
        int count=bookRepository.deleteByName(name);
        return count > 0 ? true : false;
    }


}
